package leecode

import "sort"

func relativeSortArray(arr1 []int, arr2 []int) []int {
	hashMap := make(map[int]int)  //key为arr2对应元素的下标
	hashArr1 := make(map[int]int) //key为arr1中的元素 , value为对应次数
	for i := 0; i < len(arr2); i++ {
		hashMap[i] = arr2[i]
	}
	for i := 0; i < len(arr1); i++ {
		hashArr1[arr1[i]]++
	}
	var result []int
	var temp []int
	for i := 0; i < len(hashMap); i++ {
		count := hashArr1[hashMap[i]]
		delete(hashArr1, hashMap[i])
		for j := 0; j < count; j++ {
			result = append(result, hashMap[i])
		}
	}
	for key, value := range hashArr1 {
		for i := 0; i < value; i++ {
			temp = append(temp, key)
		}
	}
	sort.Ints(temp)
	result = append(result, temp...)
	return result
}
